1
|
|
|
import merge from "../help/merge.js"; |
2
|
|
|
|
3
|
|
|
function getEncodingHeight(encoding, options){ |
4
|
|
|
return options.height + |
5
|
|
|
((options.displayValue && encoding.text.length > 0) ? options.fontSize + options.textMargin : 0) + |
6
|
|
|
options.marginTop + |
7
|
|
|
options.marginBottom; |
8
|
|
|
} |
9
|
|
|
|
10
|
|
|
function getBarcodePadding(textWidth, barcodeWidth, options){ |
11
|
|
|
if(options.displayValue && barcodeWidth < textWidth){ |
12
|
|
|
if(options.textAlign == "center"){ |
13
|
|
|
return Math.floor((textWidth - barcodeWidth) / 2); |
14
|
|
|
} |
15
|
|
|
else if(options.textAlign == "left"){ |
16
|
|
|
return 0; |
17
|
|
|
} |
18
|
|
|
else if(options.textAlign == "right"){ |
19
|
|
|
return Math.floor(textWidth - barcodeWidth); |
20
|
|
|
} |
21
|
|
|
} |
22
|
|
|
return 0; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
function calculateEncodingAttributes(encodings, barcodeOptions, context){ |
26
|
|
|
for(let i = 0; i < encodings.length; i++){ |
27
|
|
|
var encoding = encodings[i]; |
28
|
|
|
var options = merge(barcodeOptions, encoding.options); |
29
|
|
|
|
30
|
|
|
// Calculate the width of the encoding |
31
|
|
|
var textWidth; |
32
|
|
|
if(options.displayValue){ |
33
|
|
|
textWidth = messureText(encoding.text, options, context); |
34
|
|
|
} |
35
|
|
|
else{ |
36
|
|
|
textWidth = 0; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
var barcodeWidth = encoding.data.length * options.width; |
40
|
|
|
encoding.width = Math.ceil(Math.max(textWidth, barcodeWidth)); |
41
|
|
|
|
42
|
|
|
encoding.height = getEncodingHeight(encoding, options); |
43
|
|
|
|
44
|
|
|
encoding.barcodePadding = getBarcodePadding(textWidth, barcodeWidth, options); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
function getTotalWidthOfEncodings(encodings){ |
49
|
|
|
var totalWidth = 0; |
50
|
|
|
for(let i = 0; i < encodings.length; i++){ |
51
|
|
|
totalWidth += encodings[i].width; |
52
|
|
|
} |
53
|
|
|
return totalWidth; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
function getMaximumHeightOfEncodings(encodings){ |
57
|
|
|
var maxHeight = 0; |
58
|
|
|
for(let i = 0; i < encodings.length; i++){ |
59
|
|
|
if(encodings[i].height > maxHeight){ |
60
|
|
|
maxHeight = encodings[i].height; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
return maxHeight; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
function messureText(string, options, context){ |
67
|
|
|
var ctx; |
68
|
|
|
|
69
|
|
|
if(context){ |
70
|
|
|
ctx = context; |
71
|
|
|
} |
72
|
|
|
else if(typeof document !== "undefined"){ |
73
|
|
|
ctx = document.createElement("canvas").getContext("2d"); |
74
|
|
|
} |
75
|
|
|
else{ |
76
|
|
|
// If the text cannot be messured we will return 0. |
77
|
|
|
// This will make some barcode with big text render incorrectly |
78
|
|
|
return 0; |
79
|
|
|
} |
80
|
|
|
ctx.font = options.fontOptions + " " + options.fontSize + "px " + options.font; |
81
|
|
|
|
82
|
|
|
// Calculate the width of the encoding |
83
|
|
|
var size = ctx.measureText(string).width; |
84
|
|
|
|
85
|
|
|
return size; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
export {getMaximumHeightOfEncodings, getEncodingHeight, getBarcodePadding, calculateEncodingAttributes, getTotalWidthOfEncodings}; |
89
|
|
|
|